﻿using HslCommunication;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 扫码枪应用项目
{
    public class HistoryDataService
    {


        public bool AddHistoryData(HistoryData historyData)
        {
            string sql = "Insert into History(InsertTime,ProductNumber,Result)" +
                " values(@InsertTime,@ProductNumber,@Result)";

            SQLiteParameter[] parameters = new SQLiteParameter[]
            {
                new SQLiteParameter("@InsertTime", historyData.InsertTime.ToString("yyyy-MM-dd HH:mm:ss")),
                new SQLiteParameter("@ProductNumber", historyData.ProductNumber),
                new SQLiteParameter("@Result", historyData.Result),
                    };
            return SQLiteHelper.ExecuteNonQuery(sql, parameters) == 1;
        }

        public bool UpdateHistoryData(HistoryData historyData)
        {
            string sql = "Update History Set InsertTime=@InsertTime," +
                "ProductNumber=@ProductNumber,Result=@Result where ProductNumber=@ProductNumber";
            SQLiteParameter[] parameters = new SQLiteParameter[]
          {
                    new SQLiteParameter("@InsertTime", historyData.InsertTime.ToString("yyyy-MM-dd HH:mm:ss")),
                    new SQLiteParameter("@ProductNumber", historyData.ProductNumber),
                    new SQLiteParameter("@Result", historyData.Result),

                  };
            return SQLiteHelper.ExecuteNonQuery(sql, parameters) == 1;
        }
        public bool DeleteHistoryData(HistoryData historyData, string info)
        {
            string sql = "Delete  from history where ProductNumber=@Info";
            SQLiteParameter[] parameters = new SQLiteParameter[]
                   {
                    new SQLiteParameter("@Info",info),
                   };
            return SQLiteHelper.ExecuteNonQuery(sql, parameters) == 1;
        }

        //查询数据

        public OperateResult<List<HistoryData>> GetHistoryDataByTime(DateTime start, DateTime end)
        {

            string sql = "Select InsertTime,ProductNumber,Result from History where InsertTime between @Start and @End";
            SQLiteParameter[] parameters = new SQLiteParameter[]
                   {
                    new SQLiteParameter("@Start",start),
                    new SQLiteParameter("@End",end),

                   };

            SQLiteDataReader dataReader = SQLiteHelper.ExecuteReader(sql, parameters);
            List<HistoryData> historyDatas = new List<HistoryData>();
            while (dataReader.Read())
            {
                historyDatas.Add(new HistoryData()
                {
                    InsertTime = Convert.ToDateTime(dataReader["InsertTime"]),
                    ProductNumber = Convert.ToString(dataReader["ProductNumber"]),
                    Result = Convert.ToString(dataReader["Result"]),

                });
            }
            dataReader.Close();
            return OperateResult.CreateSuccessResult(historyDatas);
        }
        public OperateResult<List<HistoryData>> GetHistoryDataByInfo(string info)
        {

            string sql = "Select InsertTime,ProductNumber,Result from History where ProductNumber=@Info";
            SQLiteParameter[] parameters = new SQLiteParameter[]
                   {
                    new SQLiteParameter("@Info",info),
                   };

            SQLiteDataReader dataReader = SQLiteHelper.ExecuteReader(sql, parameters);
            List<HistoryData> historyDatas = new List<HistoryData>();
            while (dataReader.Read())
            {
                historyDatas.Add(new HistoryData()
                {
                    InsertTime = Convert.ToDateTime(dataReader["InsertTime"]),
                    ProductNumber = Convert.ToString(dataReader["ProductNumber"]),
                    Result = Convert.ToString(dataReader["Result"]),
                });
            }
            dataReader.Close();
            return OperateResult.CreateSuccessResult(historyDatas);
        }



        public List<HistoryData> GetAllHistoryDataByInfo(string info)
        {

            string sql = "Select InsertTime,ProductNumber,Result from History";


            SQLiteDataReader dataReader = SQLiteHelper.ExecuteReader(sql);
            List<HistoryData> historyDatas = new List<HistoryData>();
            while (dataReader.Read())
            {
                historyDatas.Add(new HistoryData()
                {
                    InsertTime = Convert.ToDateTime(dataReader["InsertTime"]),
                    ProductNumber = Convert.ToString(dataReader["ProductNumber"]),
                    Result = Convert.ToString(dataReader["Result"]),

                });
            }
            dataReader.Close();
            return historyDatas;
        }

    }

}


